<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'jh:plan:jhPlanArea:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="70%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <div>
      <BasicTable @register="registerRdOrderRdsTable" @row-click="handleRdOrderRdsRowClick">
        <!-- <template #headerCell="{ column }">
          <template v-if="column.dataIndex === 'tzQty'">
            <span class="fontWeight-bold">
              {{ column.customTitle }}
            </span>
          </template>
          <template v-else>
            {{ column.customTitle }}
          </template>
        </template>
        <template #summary>
          <Table.Summary fixed>
            <Table.Summary.Row>
              <Table.Summary.Cell :index="0" :col-span="1" :style="{ textAlign: 'center' }"
                >合计</Table.Summary.Cell
              >
              <Table.Summary.Cell :index="2" :col-span="3"></Table.Summary.Cell>
              <Table.Summary.Cell :index="10" :style="{ textAlign: 'left', fontWeight: 'bold' }">
                {{ totalIqty.fqty }}</Table.Summary.Cell
              >
              <Table.Summary.Cell :index="11" :style="{ textAlign: 'left', fontWeight: 'bold' }">
                {{ totalIqty.xcQty }}</Table.Summary.Cell
              >
              <Table.Summary.Cell :index="12" :style="{ textAlign: 'left', fontWeight: 'bold' }">
                {{ totalIqty.syBhQty }}</Table.Summary.Cell>
              
              <Table.Summary.Cell :index="12" :style="{ textAlign: 'left', fontWeight: 'bold' }">
                  {{ totalIqty.sumpickQty }}</Table.Summary.Cell>
                
              <Table.Summary.Cell :index="13" :style="{ textAlign: 'left', fontWeight: 'bold' }">
                {{ totalIqty.tzQty }}
              </Table.Summary.Cell>
            </Table.Summary.Row>
          </Table.Summary>
        </template> -->
      </BasicTable>
    </div>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsRdRd32Rdrecord32TzList',
  });
</script>
<script lang="ts" setup>
  import { unref, defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { companyTreeData } from '/@/api/sys/company';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { Table } from 'ant-design-vue';
  import {
    findCidsNotSendByOrder,
    orderRkBack
  } from '/@/api/wms/jh/pick/jhPickMain';
  const { t } = useI18n('wms.rd.order.rdOrder');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const emit = defineEmits(['success', 'register']);
  const groupFlag = ref<string>('');
  const record = ref<RdOrder>({} as RdOrder);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: t('推送撤回'),
  };
  const [registerRdOrderRdsTable, rdOrderRdsTable] = useTable({
    // actionColumn: {
    //   width: 60,
    //   actions: (record: Recordable) => [
    //     {
    //       icon: 'i-ant-design:delete-outlined',
    //       color: 'error',
    //       popConfirm: {
    //         title: '是否确认删除',
    //         confirm: handleRdOrderRdsDelete.bind(this, record),
    //       },
    //       auth: 'wms:rd:order:rdOrder:edit',
    //     },
    //   ],
    // },
    rowKey: 'id',
    pagination: false,
    bordered: true,
    size: 'small',
    inset: true,
  });

  async function setRdOrderRdsTableData(_res: Recordable) {
    rdOrderRdsTable.setColumns([
      // {
      //   title: t('发货单行号'),
      //   dataIndex: 'fentryid',
      //   width: 100,
      //   align: 'center',
      //   editRow: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 64,
      //   },
      //   editRule: false,
      // },
      {
        title: t('出库单号'),
        dataIndex: 'parent.djno',
        width: 130,
        align: 'left',
      },
      {
        title: t('订单号'),
        dataIndex: 'forderno',
        width: 130,
        align: 'left',
      },
      // {
      //   title: t('订单行号'),
      //   dataIndex: 'forderentryid',
      //   width: 100,
      //   align: 'center',
      //   editRow: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 20,
      //   },
      //   editRule: false,
      // },
      {
        title: t('物料代码'),
        dataIndex: 'fitemno',
        width: 150,
        align: 'left',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 100,
        },
        editRule: false,
      },
      {
        title: t('物料名称'),
        dataIndex: 'fitemname',
        width: 160,
        align: 'left',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 255,
        },
        editRule: false,
      },
      {
        title: t('形象刊'),
        dataIndex: 'freeVO.cfree1',
        width: 130,
        align: 'left',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 100,
        },
        editRule: false,
      },
      {
        title: t('数量'),
        dataIndex: 'fqty',
        width: 90,
        align: 'center',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 16,
        },
        editRule: false,
      },
      {
        title: t('重量'),
        dataIndex: 'fweight',
        width: 90,
        align: 'center',
        editRow: false,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 16,
        },
        editRule: false,
      },

      {
        title: t('撤回数量'),
        dataIndex: 'backQty',
        width: 90,
        align: 'center',
        editRow: true,
        // editable: true,
        editComponent: 'InputNumber',
        editComponentProps: {
          min:0,
          maxlength: 16,
        },
        editRule: true,
      },
     
      // {
      //   title: t('仓库'),
      //   dataIndex: 'fstockname',
      //   width: 130,
      //   align: 'left',
      //   editRow: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 255,
      //   },
      //   editRule: false,
      // },
      // {
      //   title: t('要求到货日期'),
      //   dataIndex: 'freturndate',
      //   width: 150,
      //   align: 'center',
      //   editRow: false,
      //   editComponent: 'DatePicker',
      //   editComponentProps: {
      //     format: 'YYYY-MM-DD HH:mm',
      //     showTime: { format: 'HH:mm' },
      //   },
      //   editRule: false,
      // },
      // {
      //   title: t('是否样书'),
      //   dataIndex: 'ifys',
      //   width: 100,
      //   align: 'left',
      //   editRow: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 20,
      //   },
      //   editRule: false,
      // },
      // {
      //   title: t('累计出库数'),
      //   dataIndex: 'sumCkQty',
      //   width: 100,
      //   align: 'center',
      //   editRow: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 16,
      //   },
      //   editRule: false,
      // },
      // {
      //   title: t('累计备货数'),
      //   dataIndex: 'sumBhQty',
      //   width: 100,
      //   align: 'center',
      //   editRow: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 16,
      //   },
      //   editRule: false,
      // },
      // {
      //   title: t('累计拣货数'),
      //   dataIndex: 'sumPickQty',
      //   width: 100,
      //   align: 'center',
      //   editRow: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 16,
      //   },
      //   editRule: false,
      // },
      // {
      //   title: t('备货状态'),
      //   dataIndex: 'bhStatus',
      //   width: 100,
      //   align: 'left',
      //   editRow: false,
      //   editComponent: 'Input',
      //   editComponentProps: {
      //     maxlength: 20,
      //   },
      //   editRule: false,
      //   dictType: 'jh_bh_status',
      // },
     
    ]);
    rdOrderRdsTable.setTableData(record.value || []);
  }

  function handleRdOrderRdsRowClick(record: Recordable) {
    record.onEdit?.(true, false);
  }

  function handleRdOrderRdsAdd() {
    rdOrderRdsTable.insertTableDataRecord({
      id: new Date().getTime(),
      isNewRecord: true,
      editable: true,
    });
  }

  function handleRdOrderRdsDelete(record: Recordable) {
    rdOrderRdsTable.deleteTableDataRecord(record);
  }

  async function getRdOrderRdsList() {
    let rdOrderRdsListValid = true;
    let rdOrderRdsList: Recordable[] = [];
    for (const record of rdOrderRdsTable.getDataSource()) {
      if (!(await record.onEdit?.(false, true))) {
        rdOrderRdsListValid = false;
      }
      rdOrderRdsList.push({
        ...record,
        id: !!record.isNewRecord ? '' : record.id,
      });
    }
    for (const record of rdOrderRdsTable.getDelDataSource()) {
      if (!!record.isNewRecord) continue;
      rdOrderRdsList.push({
        ...record,
        status: '1',
      });
    }
    if (!rdOrderRdsListValid) {
      throw { errorFields: [{ name: ['rdOrderRdsList'] }] };
    }
    return rdOrderRdsList;
  }

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    // let obj = {
    //   ...data,
    //   pageNo:1,
    //   pageSize:999
    // }
    // const res = await findHandleYtsData(obj);
    const list = await findCidsNotSendByOrder(data);
    record.value = list.map(item=>{
      item.backQty = item.syBackQty
      return item
    })
    // record.value.__t = new Date().getTime();
    setRdOrderRdsTableData();
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      // const data = await validate();
      const data = {};
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: true,
        // djno: record.value.djno,
      };
      data.rdOrderRdsList = await getRdOrderRdsList();
      // console.log('submit', params, data, record);
      const res = await orderRkBack(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }

  const totalIqty = computed(() => {
    // 计算总报工数
    const tableData = getDataSource();
    console.log('tableData', tableData);
    // const tableData = getDataSource.value;
    let sumFqty = tableData.reduce((total, item) => total + (item.fqty || 0), 0);
    let sumXcQty = tableData.reduce((total, item) => total + (item.xcQty || 0), 0);
    let sumSyBhQty = tableData.reduce((total, item) => total + (item.xclDiffQty || 0), 0);
    let sumpickQty = tableData.reduce((total, item) => total + (item.pickQty || 0), 0);
    
    let sumTzQty = tableData.reduce((total, item) => total + (Number(item.editValueRefs.tzQty) || 0), 0);
    console.log('sumTzQty', sumTzQty);
    return {
      fqty: sumFqty,
      xcQty: sumXcQty,
      syBhQty: sumSyBhQty,
      sumpickQty: sumpickQty,
      tzQty: sumTzQty,
    };
  });
</script>
<style scoped>
  .ant-input{
    border: 1px solid red !important;
  }
  .fontWeight-bold{
    font-weight: bold;
  }

  :deep(.jeesite-editable-cell__action) {
    display: none;
  }

  :deep(.edit-cell-align-center) {
    width: 100% !important;
  }
</style>
